home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevmpla.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  18.4 KB  |  585 lines

  1. /* Copyright (C) 1993, 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevmpla.c,v 1.2 2000/09/19 19:00:14 lpd Exp $ */
  20. /* Any-depth planar "memory" (stored bitmap) device */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsbitops.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"        /* semi-public definitions */
  27. #include "gxgetbit.h"
  28. #include "gdevmem.h"        /* private definitions */
  29. #include "gdevmpla.h"        /* interface */
  30.  
  31. /* procedures */
  32. private dev_proc_open_device(mem_planar_open);
  33. declare_mem_procs(mem_planar_copy_mono, mem_planar_copy_color, mem_planar_fill_rectangle);
  34. private dev_proc_strip_tile_rectangle(mem_planar_strip_tile_rectangle);
  35. private dev_proc_get_bits_rectangle(mem_planar_get_bits_rectangle);
  36.  
  37. /*
  38.  * Set up a planar memory device, after calling gs_make_mem_device but
  39.  * before opening the device.  The pre-existing device provides the color
  40.  * mapping procedures, but not the drawing procedures.  Requires: num_planes
  41.  * > 0, plane_depths[0 ..  num_planes - 1] > 0, sum of plane depths =
  42.  * mdev->color_info.depth.
  43.  *
  44.  * Note that this is the only public procedure in this file, and the only
  45.  * sanctioned way to set up a planar memory device.
  46.  */
  47. int
  48. gdev_mem_set_planar(gx_device_memory * mdev, int num_planes,
  49.             const gx_render_plane_t *planes /*[num_planes]*/)
  50. {
  51.     int total_depth;
  52.     int same_depth = planes[0].depth;
  53.     gx_color_index covered = 0;
  54.     int pi;
  55.  
  56.     if (num_planes < 1 || num_planes > GX_DEVICE_COLOR_MAX_COMPONENTS)
  57.     return_error(gs_error_rangecheck);
  58.     for (pi = 0, total_depth = 0; pi < num_planes; ++pi) {
  59.     int shift = planes[pi].shift;
  60.     int plane_depth = planes[pi].depth;
  61.     gx_color_index mask;
  62.  
  63.     if (shift < 0 || plane_depth > 16 ||
  64.         !gdev_mem_device_for_bits(plane_depth))
  65.         return_error(gs_error_rangecheck);
  66.     mask = (((gx_color_index)1 << plane_depth) - 1) << shift;
  67.     if (covered & mask)
  68.         return_error(gs_error_rangecheck);
  69.     covered |= mask;
  70.     if (plane_depth != same_depth)
  71.         same_depth = 0;
  72.     total_depth += plane_depth;
  73.     }
  74.     if (total_depth > mdev->color_info.depth)
  75.     return_error(gs_error_rangecheck);
  76.     mdev->num_planes = num_planes;
  77.     memcpy(mdev->planes, planes, num_planes * sizeof(planes[0]));
  78.     mdev->plane_depth = same_depth;
  79.     /* Change the drawing procedures. */
  80.     set_dev_proc(mdev, open_device, mem_planar_open);
  81.     set_dev_proc(mdev, fill_rectangle, mem_planar_fill_rectangle);
  82.     set_dev_proc(mdev, copy_mono, mem_planar_copy_mono);
  83.     set_dev_proc(mdev, copy_color, mem_planar_copy_color);
  84.     set_dev_proc(mdev, copy_alpha, gx_default_copy_alpha);
  85.     set_dev_proc(mdev, strip_tile_rectangle, mem_planar_strip_tile_rectangle);
  86.     set_dev_proc(mdev, strip_copy_rop, gx_default_strip_copy_rop);
  87.     set_dev_proc(mdev, get_bits_rectangle, mem_planar_get_bits_rectangle);
  88.     return 0;
  89. }
  90.  
  91. /* Open a planar memory device. */
  92. private int
  93. mem_planar_open(gx_device * dev)
  94. {
  95.     gx_device_memory *const mdev = (gx_device_memory *)dev;
  96.  
  97.     /* Check that we aren't trying to open a chunky device as planar. */
  98.     if (mdev->num_planes == 0)
  99.     return_error(gs_error_rangecheck);
  100.     return gdev_mem_open_scan_lines(mdev, dev->height);
  101. }
  102.  
  103. /*
  104.  * We execute drawing operations by patching a few parameters in the
  105.  * device structure and then calling the procedure appropriate to the
  106.  * plane depth.
  107.  */
  108. typedef struct mem_save_params_s {
  109.     int depth;            /* color_info.depth */
  110.     byte *base;
  111.     byte **line_ptrs;
  112. } mem_save_params_t;
  113. #define MEM_SAVE_PARAMS(mdev, msp)\
  114.   (msp.depth = mdev->color_info.depth,\
  115.    msp.base = mdev->base,\
  116.    msp.line_ptrs = mdev->line_ptrs)
  117. #define MEM_SET_PARAMS(mdev, plane_depth)\
  118.   (mdev->color_info.depth = plane_depth, /* maybe not needed */\
  119.    mdev->base = mdev->line_ptrs[0],\
  120.    mdev->raster = bitmap_raster(mdev->width * plane_depth))
  121. #define MEM_RESTORE_PARAMS(mdev, msp)\
  122.   (mdev->color_info.depth = msp.depth,\
  123.    mdev->base = msp.base,\
  124.    mdev->line_ptrs = msp.line_ptrs)
  125.  
  126. /* Fill a rectangle with a color. */
  127. private int
  128. mem_planar_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  129.               gx_color_index color)
  130. {
  131.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  132.     mem_save_params_t save;
  133.     int pi;
  134.  
  135.     MEM_SAVE_PARAMS(mdev, save);
  136.     for (pi = 0; pi < mdev->num_planes; ++pi) {
  137.     int plane_depth = mdev->planes[pi].depth;
  138.     gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
  139.     const gx_device_memory *mdproto =
  140.         gdev_mem_device_for_bits(plane_depth);
  141.  
  142.     MEM_SET_PARAMS(mdev, plane_depth);
  143.     dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h,
  144.                       (color >> mdev->planes[pi].shift) &
  145.                       mask);
  146.     mdev->line_ptrs += mdev->height;
  147.     }
  148.     MEM_RESTORE_PARAMS(mdev, save);
  149.     return 0;
  150. }
  151.  
  152. /* Copy a bitmap. */
  153. private int
  154. mem_planar_copy_mono(gx_device * dev, const byte * base, int sourcex,
  155.              int sraster, gx_bitmap_id id, int x, int y, int w, int h,
  156.              gx_color_index color0, gx_color_index color1)
  157. {
  158.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  159.     mem_save_params_t save;
  160.     int pi;
  161.  
  162.     MEM_SAVE_PARAMS(mdev, save);
  163.     for (pi = 0; pi < mdev->num_planes; ++pi) {
  164.     int plane_depth = mdev->planes[pi].depth;
  165.     int shift = mdev->planes[pi].shift;
  166.     gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
  167.     const gx_device_memory *mdproto =
  168.         gdev_mem_device_for_bits(plane_depth);
  169.     gx_color_index c0 = 
  170.         (color0 == gx_no_color_index ? gx_no_color_index :
  171.          (color0 >> shift) & mask);
  172.     gx_color_index c1 = 
  173.         (color1 == gx_no_color_index ? gx_no_color_index :
  174.          (color1 >> shift) & mask);
  175.  
  176.     MEM_SET_PARAMS(mdev, plane_depth);
  177.     if (c0 == c1)
  178.         dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h, c0);
  179.     else
  180.         dev_proc(mdproto, copy_mono)
  181.         (dev, base, sourcex, sraster, id, x, y, w, h, c0, c1);
  182.     mdev->line_ptrs += mdev->height;
  183.     }
  184.     MEM_RESTORE_PARAMS(mdev, save);
  185.     return 0;
  186. }
  187.  
  188. /* Copy a color bitmap. */
  189. /* This is slow and messy. */
  190. private int
  191. mem_planar_copy_color(gx_device * dev, const byte * base, int sourcex,
  192.               int sraster, gx_bitmap_id id,
  193.               int x, int y, int w, int h)
  194. {
  195.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  196. #define BUF_LONGS 100    /* arbitrary, >= 1 */
  197. #define BUF_BYTES (BUF_LONGS * ARCH_SIZEOF_LONG)
  198.     union b_ {
  199.     ulong l[BUF_LONGS];
  200.     byte b[BUF_BYTES];
  201.     } buf;
  202.     int source_depth = dev->color_info.depth;
  203.     mem_save_params_t save;
  204.     int pi;
  205.  
  206.     fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
  207.     MEM_SAVE_PARAMS(mdev, save);
  208.     for (pi = 0; pi < mdev->num_planes; ++pi) {
  209.     int plane_depth = mdev->planes[pi].depth;
  210.     int shift = mdev->planes[pi].shift;
  211.     gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
  212.     const gx_device_memory *mdproto =
  213.         gdev_mem_device_for_bits(plane_depth);
  214.     /*
  215.      * Divide up the transfer into chunks that can be assembled
  216.      * within the fixed-size buffer.  This code can be simplified
  217.      * a lot if all planes have the same depth, by simply using
  218.      * copy_color to transfer one column at a time, but it might
  219.      * be very inefficient.
  220.      */
  221.     uint plane_raster = bitmap_raster(plane_depth * w);
  222.     int br, bw, bh, cx, cy, cw, ch, ix, iy;
  223.  
  224.     MEM_SET_PARAMS(mdev, plane_depth);
  225.     if (plane_raster > BUF_BYTES) {
  226.         br = BUF_BYTES;
  227.         bw = BUF_BYTES * 8 / plane_depth;
  228.         bh = 1;
  229.     } else {
  230.         br = plane_raster;
  231.         bw = w;
  232.         bh = BUF_BYTES / plane_raster;
  233.     }
  234.     /*
  235.      * We could do the extraction with get_bits_rectangle
  236.      * selecting a single plane, but this is critical enough
  237.      * code that we more or less replicate it here.
  238.      */
  239.     for (cy = y; cy < y + h; cy += ch) {
  240.         ch = min(bh, y + h - cy);
  241.         for (cx = x; cx < x + w; cx += cw) {
  242.         int sx = sourcex + cx - x;
  243.         const byte *source_base = base + sraster * (cy - y);
  244.         int source_bit = 0;
  245.  
  246.         cw = min(bw, x + w - cx);
  247.         if (sx) {
  248.             int xbit = sx * source_depth;
  249.  
  250.             source_base += xbit >> 3;
  251.             source_bit = xbit & 7;
  252.         }
  253.         for (iy = 0; iy < ch; ++iy) {
  254.             sample_load_declare_setup(sptr, sbit, source_base,
  255.                           source_bit, source_depth);
  256.             sample_store_declare_setup(dptr, dbit, dbbyte,
  257.                            buf.b + br * iy,
  258.                            0, plane_depth);
  259.  
  260.             for (ix = 0; ix < cw; ++ix) {
  261.             gx_color_index value;
  262.  
  263.             sample_load_next32(value, sptr, sbit, source_depth);
  264.             value = (value >> shift) & mask;
  265.             sample_store_next16(value, dptr, dbit, plane_depth,
  266.                         dbbyte);
  267.             }
  268.             sample_store_flush(dptr, dbit, plane_depth, dbbyte);
  269.             source_base += sraster;
  270.         }
  271.         /*
  272.          * Detect and bypass the possibility that copy_color is
  273.          * defined in terms of copy_mono.
  274.          */
  275.         if (plane_depth == 1)
  276.             dev_proc(mdproto, copy_mono)
  277.             (dev, buf.b, 0, br, gx_no_bitmap_id, cx, cy, cw, ch,
  278.              (gx_color_index)0, (gx_color_index)1);
  279.         else
  280.             dev_proc(mdproto, copy_color)
  281.             (dev, buf.b, 0, br, gx_no_bitmap_id, cx, cy, cw, ch);
  282.         }
  283.     }
  284.     mdev->line_ptrs += mdev->height;
  285.     }
  286.     MEM_RESTORE_PARAMS(mdev, save);
  287.     return 0;
  288. #undef BUF_BYTES
  289. #undef BUF_LONGS
  290. }
  291.  
  292. private int
  293. mem_planar_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  294.                 int x, int y, int w, int h,
  295.                 gx_color_index color0, gx_color_index color1,
  296.                 int px, int py)
  297. {
  298.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  299.     mem_save_params_t save;
  300.     int pi;
  301.  
  302.     /* We can't split up the transfer if the tile is colored. */
  303.     if (color0 == gx_no_color_index && color1 == gx_no_color_index)
  304.     return gx_default_strip_tile_rectangle
  305.         (dev, tiles, x, y, w, h, color0, color1, px, py);
  306.     MEM_SAVE_PARAMS(mdev, save);
  307.     for (pi = 0; pi < mdev->num_planes; ++pi) {
  308.     int plane_depth = mdev->planes[pi].depth;
  309.     int shift = mdev->planes[pi].shift;
  310.     gx_color_index mask = ((gx_color_index)1 << plane_depth) - 1;
  311.     const gx_device_memory *mdproto =
  312.         gdev_mem_device_for_bits(plane_depth);
  313.     gx_color_index c0 = 
  314.         (color0 == gx_no_color_index ? gx_no_color_index :
  315.          (color0 >> shift) & mask);
  316.     gx_color_index c1 = 
  317.         (color1 == gx_no_color_index ? gx_no_color_index :
  318.          (color1 >> shift) & mask);
  319.  
  320.     MEM_SET_PARAMS(mdev, plane_depth);
  321.     if (c0 == c1)
  322.         dev_proc(mdproto, fill_rectangle)(dev, x, y, w, h, c0);
  323.     else {
  324.         /*
  325.          * Temporarily replace copy_mono in case strip_tile_rectangle is
  326.          * defined in terms of it.
  327.          */
  328.         set_dev_proc(dev, copy_mono, dev_proc(mdproto, copy_mono));
  329.         dev_proc(mdproto, strip_tile_rectangle)
  330.         (dev, tiles, x, y, w, h, c0, c1, px, py);
  331.     }
  332.     mdev->line_ptrs += mdev->height;
  333.     }
  334.     MEM_RESTORE_PARAMS(mdev, save);
  335.     set_dev_proc(dev, copy_mono, mem_planar_copy_mono);
  336.     return 0;
  337. }
  338.  
  339. /*
  340.  * Repack planar into chunky format.  This is an internal procedure that
  341.  * implements the straightforward chunky case of get_bits_rectangle, and
  342.  * is also used for the general cases.
  343.  */
  344. private int
  345. planar_to_chunky(gx_device_memory *mdev, int x, int y, int w, int h,
  346.          int offset, uint draster, byte *dest)
  347. {
  348.     int num_planes = mdev->num_planes;
  349.     sample_load_declare(sptr[GX_DEVICE_COLOR_MAX_COMPONENTS],
  350.             sbit[GX_DEVICE_COLOR_MAX_COMPONENTS]);
  351.     sample_store_declare(dptr, dbit, dbbyte);
  352.     int ddepth = mdev->color_info.depth;
  353.     int direct =
  354.     (mdev->color_info.depth != num_planes * mdev->plane_depth ? 0 :
  355.      mdev->planes[0].shift == 0 ? -mdev->plane_depth : mdev->plane_depth);
  356.     int pi, ix, iy;
  357.  
  358.     /* Check whether the planes are of equal size and sequential. */
  359.     /* If direct != 0, we already know they exactly fill the depth. */
  360.     if (direct < 0) {
  361.     for (pi = 0; pi < num_planes; ++pi)
  362.         if (mdev->planes[pi].shift != pi * -direct) {
  363.         direct = 0; break;
  364.         }
  365.     } else if (direct > 0) {
  366.     for (pi = 0; pi < num_planes; ++pi)
  367.         if (mdev->planes[num_planes - 1 - pi].shift != pi * direct) {
  368.         direct = 0; break;
  369.         }
  370.     }
  371.     for (iy = y; iy < y + h; ++iy) {
  372.     byte **line_ptr = mdev->line_ptrs + iy;
  373.  
  374.     for (pi = 0; pi < num_planes; ++pi, line_ptr += mdev->height) {
  375.         int plane_depth = mdev->planes[pi].depth;
  376.         int xbit = x * plane_depth;
  377.  
  378.         sptr[pi] = *line_ptr + (xbit >> 3);
  379.         sample_load_setup(sbit[pi], xbit & 7, plane_depth);
  380.     }
  381.     {
  382.         int xbit = offset * ddepth;
  383.  
  384.         dptr = dest + (iy - y) * draster + (xbit >> 3);
  385.         sample_store_setup(dbit, xbit & 7, ddepth);
  386.     }
  387.     if (direct == -8) {
  388.         /* 1 byte per component, lsb first. */
  389.         switch (num_planes) {
  390.         case 3: {
  391.         const byte *p0 = sptr[2];
  392.         const byte *p1 = sptr[1];
  393.         const byte *p2 = sptr[0];
  394.  
  395.         for (ix = w; ix > 0; --ix, dptr += 3) {
  396.             dptr[0] = *p0++;
  397.             dptr[1] = *p1++;
  398.             dptr[2] = *p2++;
  399.         }
  400.         }
  401.         continue;
  402.         case 4:
  403.         for (ix = w; ix > 0; --ix, dptr += 4) {
  404.             dptr[0] = *sptr[3]++;
  405.             dptr[1] = *sptr[2]++;
  406.             dptr[2] = *sptr[1]++;
  407.             dptr[3] = *sptr[0]++;
  408.         }
  409.         continue;
  410.         default:
  411.         break;
  412.         }
  413.     }
  414.     sample_store_preload(dbbyte, dptr, dbit, ddepth);
  415.     for (ix = w; ix > 0; --ix) {
  416.         gx_color_index color = 0;
  417.  
  418.         for (pi = 0; pi < num_planes; ++pi) {
  419.         int plane_depth = mdev->planes[pi].depth;
  420.         uint value;
  421.  
  422.         sample_load_next16(value, sptr[pi], sbit[pi], plane_depth);
  423.         color |= (gx_color_index)value << mdev->planes[pi].shift;
  424.         }
  425.         sample_store_next32(color, dptr, dbit, ddepth, dbbyte);
  426.     }
  427.     sample_store_flush(dptr, dbit, ddepth, dbbyte);
  428.     }
  429.     return 0;
  430. }
  431.  
  432. /* Copy bits back from a planar memory device. */
  433. private int
  434. mem_planar_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  435.                   gs_get_bits_params_t * params,
  436.                   gs_int_rect ** unread)
  437. {
  438.     /* This duplicates most of mem_get_bits_rectangle.  Tant pis. */
  439.     gx_device_memory * const mdev = (gx_device_memory *)dev;
  440.     gs_get_bits_options_t options = params->options;
  441.     int x = prect->p.x, w = prect->q.x - x, y = prect->p.y, h = prect->q.y - y;
  442.     int num_planes = mdev->num_planes;
  443.     gs_get_bits_params_t copy_params;
  444.     int code;
  445.  
  446.     if (options == 0) {
  447.     /*
  448.      * Unfortunately, as things stand, we have to support
  449.      * GB_PACKING_CHUNKY.  In fact, we can't even claim to support
  450.      * GB_PACKING_PLANAR, because there is currently no way to
  451.      * describe the particular planar packing format that the device
  452.      * actually stores.
  453.      */
  454.     params->options =
  455.         (GB_ALIGN_STANDARD | GB_ALIGN_ANY) |
  456.         (GB_RETURN_COPY | GB_RETURN_POINTER) |
  457.         (GB_OFFSET_0 | GB_OFFSET_SPECIFIED | GB_OFFSET_ANY) |
  458.         (GB_RASTER_STANDARD | GB_RASTER_SPECIFIED | GB_RASTER_ANY) |
  459.         /*
  460.         (mdev->num_planes == mdev->color_info.depth ?
  461.          GB_PACKING_CHUNKY | GB_PACKING_PLANAR | GB_PACKING_BIT_PLANAR :
  462.          GB_PACKING_CHUNKY | GB_PACKING_PLANAR)
  463.         */
  464.         GB_PACKING_CHUNKY |
  465.         GB_COLORS_NATIVE | GB_ALPHA_NONE;
  466.     return_error(gs_error_rangecheck);
  467.     }
  468.     if ((w <= 0) | (h <= 0)) {
  469.     if ((w | h) < 0)
  470.         return_error(gs_error_rangecheck);
  471.     return 0;
  472.     }
  473.     if (x < 0 || w > dev->width - x ||
  474.     y < 0 || h > dev->height - y
  475.     )
  476.     return_error(gs_error_rangecheck);
  477.  
  478.     /*
  479.      * If the request is for exactly one plane, hand it off to a device
  480.      * temporarily tweaked to return just that plane.
  481.      */
  482.     if (!(~options & (GB_PACKING_PLANAR | GB_SELECT_PLANES))) {
  483.     /* Check that only a single plane is being requested. */
  484.     int pi;
  485.  
  486.     for (pi = 0; pi < num_planes; ++pi)
  487.         if (params->data[pi] != 0)
  488.         break;
  489.     if (pi < num_planes) {
  490.         int plane = pi++;
  491.  
  492.         for (; pi < num_planes; ++pi)
  493.         if (params->data[pi] != 0)
  494.             break;
  495.         if (pi == num_planes) {
  496.         mem_save_params_t save;
  497.  
  498.         copy_params = *params;
  499.         copy_params.options =
  500.             (options & ~(GB_PACKING_ALL | GB_SELECT_PLANES)) |
  501.             GB_PACKING_CHUNKY;
  502.         copy_params.data[0] = copy_params.data[plane];
  503.         MEM_SAVE_PARAMS(mdev, save);
  504.         mdev->line_ptrs += mdev->height * plane;
  505.         MEM_SET_PARAMS(mdev, mdev->planes[plane].depth);
  506.         code = mem_get_bits_rectangle(dev, prect, ©_params,
  507.                           unread);
  508.         MEM_RESTORE_PARAMS(mdev, save);
  509.         if (code >= 0) {
  510.             params->data[plane] = copy_params.data[0];
  511.             return code;
  512.         }
  513.         }
  514.     }
  515.     }
  516.     /*
  517.      * We can't return the requested plane by itself.  Fall back to
  518.      * chunky format.  This is somewhat painful.
  519.      *
  520.      * The code here knows how to produce just one chunky format:
  521.      * GB_COLORS_NATIVE, GB_ALPHA_NONE, GB_RETURN_COPY.
  522.      * For any other format, we generate this one in a buffer and
  523.      * hand it off to gx_get_bits_copy.  This is *really* painful.
  524.      */
  525.     if (!(~options & (GB_COLORS_NATIVE | GB_ALPHA_NONE |
  526.               GB_PACKING_CHUNKY | GB_RETURN_COPY))) {
  527.     int offset = (options & GB_OFFSET_SPECIFIED ? params->x_offset : 0);
  528.     uint draster =
  529.         (options & GB_RASTER_SPECIFIED ? params->raster :
  530.          bitmap_raster((offset + w) * mdev->color_info.depth));
  531.  
  532.     planar_to_chunky(mdev, x, y, w, h, offset, draster, params->data[0]);
  533.     } else {
  534.     /*
  535.      * Do the transfer through an intermediate buffer.
  536.      * The buffer must be large enough to hold at least one pixel,
  537.      * i.e., GX_DEVICE_COLOR_MAX_COMPONENTS 16-bit values.
  538.      * The algorithms are very similar to those in copy_color.
  539.      */
  540. #define BUF_LONGS\
  541.   max(100, (GX_DEVICE_COLOR_MAX_COMPONENTS * 2 + sizeof(long) - 1) /\
  542.       sizeof(long))
  543. #define BUF_BYTES (BUF_LONGS * ARCH_SIZEOF_LONG)
  544.     union b_ {
  545.         ulong l[BUF_LONGS];
  546.         byte b[BUF_BYTES];
  547.     } buf;
  548.     int br, bw, bh, cx, cy, cw, ch;
  549.     int ddepth = mdev->color_info.depth;
  550.     uint raster = bitmap_raster(ddepth * mdev->width);
  551.     gs_get_bits_params_t dest_params;
  552.  
  553.     if (raster > BUF_BYTES) {
  554.         br = BUF_BYTES;
  555.         bw = BUF_BYTES * 8 / ddepth;
  556.         bh = 1;
  557.     } else {
  558.         br = raster;
  559.         bw = w;
  560.         bh = BUF_BYTES / raster;
  561.     }
  562.     copy_params.options =
  563.         GB_COLORS_NATIVE | GB_PACKING_CHUNKY | GB_ALPHA_NONE |
  564.         GB_RASTER_STANDARD;
  565.     copy_params.raster = raster;
  566.     dest_params = *params;
  567.     for (cy = y; cy < y + h; cy += ch) {
  568.         ch = min(bh, y + h - cy);
  569.         for (cx = x; cx < x + w; cx += cw) {
  570.         cw = min(bw, x + w - cx);
  571.         planar_to_chunky(mdev, cx, cy, cw, ch, 0, br, buf.b);
  572.         dest_params.x_offset = params->x_offset + cx - x;
  573.         code = gx_get_bits_copy(dev, 0, cw, ch, &dest_params,
  574.                     ©_params, buf.b, br);
  575.         if (code < 0)
  576.             return code;
  577.         }
  578.         dest_params.data[0] += ch * raster;
  579.     }
  580. #undef BUF_BYTES
  581. #undef BUF_LONGS
  582.     }
  583.     return 0;
  584. }
  585.